package com.yixing.tech.wms.statistics.service;

import com.yixing.tech.common.base.BusinessException;
import com.yixing.tech.common.enums.YesOrNo;
import com.yixing.tech.wms.statistics.domain.entity.InventoryStatistics;
import com.yixing.tech.wms.stock.out.StockOutService;
import com.yixing.tech.wms.stock.out.domain.Inventory;
import com.yixing.tech.wms.stock.out.domain.MatStorageRegulation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.bson.Document;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class InventoryStatisticsServiceImpl implements IInventoryStatisticsService {

    @Resource
    StockOutService stockOutService;

    @Override
    public void insertInventory(Inventory inventory) {
        InventoryStatistics inventoryStatistics = buildInventoryStatistics(inventory);
        inventoryStatistics.setInventoryId(inventory.getId());
        inventoryStatistics.setCreateId(inventory.getCreateId());
        inventoryStatistics.setCreateName(inventory.getCreateName());

        try {
            inventoryStatistics = stockOutService.saveInventoryStatistics(inventoryStatistics);
            log.info("保存库存统计记录:{}", inventoryStatistics);
        } catch (BusinessException e) {
            log.error("保存库存统计信息抛出异常:{}", e);
        }
    }

    @Override
    public void updateInventory(Inventory inventory) {
        InventoryStatistics inventoryStatistics = buildInventoryStatistics(inventory);

        inventoryStatistics.setIsDelete(inventory.getIsDelete());
        inventoryStatistics.setUpdateId(inventory.getUpdateId());
        inventoryStatistics.setUpdateName(inventory.getUpdateName());

        long mc = stockOutService.updateInventoryStatisticsByInventoryId(inventoryStatistics, inventory.getId());
        log.info("更新库存统计信息:content[{}],inventoryId[{}],mc[{}]", inventoryStatistics, inventory.getId(), mc);
    }

    private InventoryStatistics buildInventoryStatistics(Inventory inventory) {
        InventoryStatistics inventoryStatistics = new InventoryStatistics();
        inventoryStatistics.setWarehouseCode(inventory.getWarehouseCode());
        inventoryStatistics.setAreaCode(inventory.getAreaCode());
        inventoryStatistics.setLocationCode(inventory.getLocationCode());
        inventoryStatistics.setContainerCode(inventory.getContainerCode());

        if(ObjectUtils.isNotEmpty(inventory.getContainerCode())) {
            try {
                String podTypeId = stockOutService.queryPodType(inventory.getContainerCode());
                inventoryStatistics.setContainerType(Document.parse(String.format("{id: ObjectId(\"%s\")}", podTypeId)));
            } catch (BusinessException e) {
                log.error("查询容器类型抛出异常:{}", e);
            }
        }

        inventoryStatistics.setMaterialCode(inventory.getMaterialCode());
        inventoryStatistics.setColorName(inventory.getColorName());
        inventoryStatistics.setLanguageName(inventory.getLanguageName());
        inventoryStatistics.setBatchNo(inventory.getBatchNo());
        inventoryStatistics.setRealNum(inventory.getRealNum());
        inventoryStatistics.setPartitionSide(inventory.getPartitionSide());

        try {
            MatStorageRegulation matStorageRegulation = stockOutService.queryMatStorageRegulation(inventory.getMaterialCode());
            if(ObjectUtils.isNotEmpty(matStorageRegulation)) {
                // 计算储存占比
                inventoryStatistics.setStorageQuantity(matStorageRegulation.getCapacity());
                double proportion = inventory.getRealNum() / matStorageRegulation.getCapacity();
                proportion = Double.parseDouble(String.format("%.2f", proportion));
                inventoryStatistics.setStorageProportion(proportion);
            }
        } catch (BusinessException e) {
            log.error("查询物料存储规则抛出异常:{}", e);
        }

        return inventoryStatistics;
    }

    @Override
    public void removeInventory(Inventory inventory) {
        InventoryStatistics inventoryStatistics = new InventoryStatistics();
        inventoryStatistics.setIsDelete(YesOrNo.YES.ordinal());

        long mc = stockOutService.updateInventoryStatisticsByInventoryId(inventoryStatistics, inventory.getId());
        log.info("更新库存统计信息:content[{}],inventoryId[{}],mc[{}]", inventoryStatistics, inventory.getId(), mc);
    }

    @Override
    public void syncInventory() throws BusinessException {
        List<Inventory> inventoryList = stockOutService.queryInventoryWithoutStatistics();
        for(Inventory inventory: inventoryList) {
            try {
                log.info("库存现货统计:{}", inventory);
                insertInventory(inventory);
            }catch (Exception ex) {
                log.error("库存现货统计失败:", ex);
            }
        }
    }

}
